<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    

   public function login()
    {   

    if (IS_AJAX) {
        $map['mobile']=I("post.mobile");
        $user=M('user')->where($map)->find();
        $login_status = 'invalid';
        if (!empty($user)) {
            $login_status = 'success';
            session('home_aid',$user['id']);
            session('user',$user);
        }
         $resp['login_status'] = $login_status;
         if($login_status == 'success')
{
 
  $resp['redirect_url'] = U('Index/index');
}else{
   $resp['error'] = '注册失败';
}
echo json_encode($resp);

    }else{

       $this->display(); 
    }

        
    }
        //remote 校验用户是否注册
     public function checkuser()
    {   
        $mobile=I('post.mobile');
        $find=M('user')->where("mobile=$mobile")->find();
        if ($find) {
          echo "false";
           //exit('给用户已注册');
        }else{
            echo 'true';
        }   
    }

       //remote 校验用户是否注册
     public function check_frozen()
    {   
        $mobile=I('post.mobile');
        $find=M('user')->where("mobile=$mobile")->getField('user_frozen');
        if ($find==1) {
          echo "false";
           //exit('给用户已注册');
        }else{
            echo 'true';
        }   
    }

       //remote 校验用户身份证是否重复
     public function checkuser_idcard()
    {   
        $idcard=I('post.idcard');
        $find=M('user')->where("idcard=$idcard")->find();
        if ($find) {
          echo "false";
           //exit('给用户已注册');
        }else{
            echo 'true';
        }   
    }

         //remote 校验用户是否注册(忘记密码)
     public function checkregister()
    {   
        $mobile=I('post.mobile');
        $find=M('user')->where("mobile=$mobile")->find();
        if ($find) {
          echo "true";
           //exit('给用户已注册');
        }else{
            echo 'false';
        }   
    }

     public function register(){

         if (!IS_POST)
        {   
           
             $this->display();
        }
        else
        {   
            $invite=I('post.invite_id');
            if ($invite) { 
              $invite_id=base64_decode($invite);
                  $res=M('user')->find($invite_id);
                  if ($res) {
                   $data=$_POST;
                $data['ctime'] = time();
                $data['ip'] = get_client_ip();  
                $data['password_show'] = $data['password'] ;
                $data['password'] = md5($data['password']);
                $data['invite_id'] = $invite_id;
                $data['f_id'] = $invite_id;
                
                if ($data['yzm'] != session('valid_code')) {
                   $this->error('短信验证码错误');
                }else{
                  unset($data['yzm']);
                  $resp['submitted_data'] = $data;
$res=M('user')->add($data); //echo json_encode($resp);
            $accdata=array(
                'uid'=>$res,
                'group_id'=>1,
            );
             $user_access=M('auth_group_access_home');
            $user_access->add($accdata);
               $this->info($res);
                    $user=M('user')->find($res);
                    session('user',$user);
                    echo json_encode($resp);
                    
                }
         
              }else{
                $this->error('邀请码不存在');
              }
            }else{
              $this->error('没有邀请码，注册不成功');
            }
           
        }

    }
    

    //完善信息 传入用户id
    public function info(&$id='')
    {
         
            $user=M('user')->find($id);
             $jb=M('jbset')->find();

       switch ($user['status']) {
              case '1':
                  $months=$jb['sj_jb2_mons']-1;
                  break;
            case '2':
                  $months=$jb['sj_jb3_mons']-1;
                  break;
            case '3':
                   $months=$jb['sj_jb4_mons']-1;
                  break;
              
              default:
                  # code...
                  break;
          }

            $invite_id=$user['invite_id'];
            $end=time();
         $begin=mktime(0,0,0,date('m')-$months,1,date('Y')); 
         D('order')->getTeamTotalNumsUpCum_info($id,$begin,$end);
            if ($invite_id>0) {
              $up_invite_status=M('user')->where('id='.$invite_id)->getField('status');
              $jb=M('jbset')->find();
              switch ($up_invite_status) {
                case '1':
                 $sjcon['status']=1;
                  $con_num=$jb['sj_jb2_jb1s'];
                  $add_sj['sj_time']=time();
                  $save_sj['sj_invite']=1;
                  $sj_ordernum='sj_ordernum';

                  break;
                case '2':
                 $sjcon['status']=2;
                  $con_num=$jb['sj_jb3_jb2s'];
                   $add_sj['sj3_time']=time();
                   $save_sj['sj3_invite']=1;
                   $sj_ordernum='sj3_ordernum'; 
                   $each=10;
                  break;
                case '3':
                 $sjcon['status']=3;
                  $con_num=$jb['sj_jb4_jb3s'];
                   $add_sj['sj4_time']=time(); 
                   $save_sj['sj3_invite']=1;
                   $sj_ordernum='sj4_ordernum';
                   $each=20;
                  break;
                case '4':
                $each=40;
                  break;
                
                default:
                  # code...
                  break;
              }
             $up_invite_num=M('user')->where('invite_id='.$invite_id)->where($sjcon)->count();
             if ($up_invite_num>=$con_num) {
               $sj=M('user')->where('id='.$invite_id)->save($save_sj);
               $sj_ordernum=M('user')->where('id='.$invite_id)->getField($sj_ordernum);

               if ($sj_ordernum) {
                $sj_status=$up_invite_status>=4?$up_invite_status:$up_invite_status+1;
                   if ($sj_status>2) {
                    $add_sj['status']=$sj_status;
                    $add_sj['self_ztxf']=$invite_info['fh_ordernums']%10;
                    $add_sj['self_flordernum']=$invite_info['self_flordernum']%10;
                    $add_sj['fh_ordernums']=$invite_info['fh_ordernums']%$each;
                  }else{
                  $add_sj['status']=$sj_status;
                  $add_sj['self_ztxf']=0;
                  $add_sj['self_flordernum']=0;
                  $add_sj['fh_ordernums']=0;
                  }
                  $res_sj=M('user')->where('id='.$invite_id)->save($add_sj);

                M('auth_group_access_home')->where("uid=$invite_id")->setField("group_id",$up_invite_status);
  
                 if ($res_sj) {
                      //判断是否平级超越
                  $up_up_id=M('user')->where('id='.$invite_id)->find();
                    if ($up_up_id['invite_id']>0) {
                          $up_up_id=$up_up_id['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                        if ($add_sj['status']>=$up_up_status  ) {

                           if ($add_sj['status']==$up_up_status) {
                             M('user')->where('id='.$invite_id)->setField('f_id',0);
                          }else{
                             M('user')->where('id='.$invite_id)->setField('f_id',-1);
                             $beyond['beyond_uid']=$up_up_id;
                         $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                          $beyond['beyond_time']=time();
                         
                      M('user')->where("id=$invite_id")->save($beyond);
                      $c=M('user')->where("invite_id=$invite_id")->select();
                        foreach ($c as $key => $value) {
                           if ($value['status']<$add_sj['status']) {
                                  M('user')->where('id='.$value['id'])->setField('f_id',$value['invite_id']);
                            } 
                        }
                          }

                          

                  }
                    }
                      
             

                 }

               }
               
             } 
              $this->info($invite_id);  
            }else{
              return;
            }

           
        
    }

   

//ajax远程校验短信验证码
    public function check_sms_yzm()
    {   

      if (IS_AJAX) {
        if (I('post.yzm') != session('valid_code')) {
              echo 'false';
            }else{
                echo 'true';
              
            }
      }
         
    }

//ajax远程校验邀请码

    public function check_invite_id()
    {
      $invite=I('post.invite_id');
      $invite_id=base64_decode($invite);
      $res=M('user')->find($invite_id);
      if ($res) {
        echo "true";
      }else{
        echo "false";
      }

    }



    //退出操作
    public function outLogin()
    {
        session(null);
        $this->redirect('Login/login');
    }

    //忘记密码
    public function fogetpass()
    {
          if (!IS_POST)
        {   
            
           $this->display();
        }
        else
        {   

            
            $map['mobile'] =I('post.mobile');

            $data['password'] = md5( I('post.password2') );
            
            if (I('post.yzm') != session('valid_code')) {
               $this->error('短信验证码错误');
            }else{
                //var_dump($data);exit();
                $res=M('user')->where($map)->save($data);          
                $this->success('密码重置成功',U('Login/login'));
              
            }
           
        }
    }


         public function session()
    {
        var_dump($_SESSION);
    }
   
    public function sms_valid_code()
    {  
        $sys=M('sys')->where(array('sys_id'=>1))->find();
        $user=$sys['sms_user'];
        $pass=md5($sys['sms_pwd']);
        $mobile=I('post.m', '', 'htmlspecialchars');
      $t=I('post.t', '', 'htmlspecialchars');
      $c=rand(100000,999999);
     session('valid_code',$c);
      if (isTelNumber($mobile)) {
        $content='您的验证码为'.$c;
         $res=sendmobile($mobile,$content,$user,$pass);
         if ($res='短信发送成功') {
             exit('2');
         }
        
      }else{
        exit('-1');
      }

    }





}
